package com.zjd.association.controller;

import com.zjd.association.common.R;
import com.zjd.association.entity.Notices;
import com.zjd.association.entity.Users;
import com.zjd.association.handle.CacheHandle;
import com.zjd.association.service.NoticesService;
import com.zjd.association.service.UsersService;
import com.zjd.association.utils.UUIDUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 首页相关 控制层
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/")
public class IndexController {

    private final UsersService usersService;

    private final CacheHandle cacheHandle;

    private final NoticesService noticesService;

    /**
     * 获取通知记录
     *
     * @param token
     * @return
     */
    @GetMapping("/sys/notices")
    public R getNoticeList(String token) {
        //获取用户信息
        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
        if (user.getType() == 0) {
            List<Notices> list = noticesService.getSysNotices();
            return R.ok(list);
        } else if (user.getType() == 1) {
            List<Notices> list = noticesService.getManNotices(user.getId());
            return R.ok(list);
        } else {
            List<Notices> list = noticesService.getMemNotices(user.getId());
            return R.ok(list);
        }
    }

    /**
     * 用户登录
     *
     * @param userName 用户名
     * @param passWord 密码
     * @param session  session
     * @return token
     */
    @PostMapping("/login")
    public R login(String userName, String passWord, HttpSession session) {
        //查询用户信息
        Users user = usersService.getUserByUserName(userName);
        if (user == null) {
            return R.error("输入的用户名不存在");
        } else {
            if (passWord.equals(user.getPassWord().trim())) {
                String token = UUIDUtils.makeIDByUUID();
                //在缓存里添加下用户token
                cacheHandle.addUserCache(token, user.getId());
                return R.ok("登录成功", token);
            } else {
                return R.error("输入的密码错误");
            }
        }
    }

    /**
     * 登出
     *
     * @param token 用户token
     * @return 结果
     */
    @RequestMapping("/exit")
    public R exit(String token) {
        cacheHandle.removeUserCache(token);
        return R.ok();
    }

    /**
     * 获取用户信息
     *
     * @param token 用户token
     * @return 结果
     */
    @GetMapping("/info")
    public R info(String token) {
        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
        return R.ok(user);
    }

    /**
     * 更新用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @PostMapping("/info")
    public R info(Users user) {
        usersService.update(user);
        return R.ok();
    }

    /**
     * 更改密码时候 原始密码检查
     *
     * @param oldPwd 原始密码
     * @param token  结果
     * @return
     */
    @RequestMapping("/checkPwd")
    public R checkPwd(String oldPwd, String token) {
        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
        if (oldPwd.equals(user.getPassWord())) {
            return R.ok();
        } else {
            return R.warn("原始密码和输入密码不一致");
        }
    }

    /**
     * 更新密码
     *
     * @param token    用户token
     * @param password 密码
     * @return 结果
     */
    @PostMapping("/pwd")
    public R pwd(String token, String password) {
        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
        user.setPassWord(password);
        usersService.update(user);
        return R.ok();
    }

}